package com.gzhryc.servlet.resources;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;

import com.gzhryc.servlet.Resource;


/**
 * 文本资源类
 * @author 张浩
 * @version 1.3.2
 */
public class TextResource extends Resource{
	
	private String text;
	
	public TextResource(String mediaType,String text){
		super(mediaType);
		this.text = text;
	}

	@Override
	public void export(HttpServletRequest request,HttpServletResponse response) throws Exception{
		if(StringUtils.isNotBlank(text)){
			response.addHeader(HttpHeaders.CONTENT_TYPE,this.getMediaType());
			response.setCharacterEncoding(this.getCharset().name());
			OutputStream out = response.getOutputStream();
			out.write(text.getBytes(charset));
			out.flush();
		}
	}

}
